﻿using Inovout.Kuajing.Exports.Models;
using Inovout.Kuajing.Exports.Services;
using Inovout.Organizations.Memberships.Models;
using Inovout.Organizations.Models;
using Inovout.Organizations.Services;
using Inovout.People.Models;
using Inovout.Services;
using Inovout.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inovout.Kuajing.Exports.Console.Web.Controllers
{

    public class EmployeeController : KJBaseController<Employee>
    {
        private IEmployeeService employeeService;
        private IOrganizationUnitService organizationUnitService;
        private IExporterService exporterService;
        public EmployeeController(IEmployeeService employeeService, IOrganizationUnitService organizationUnitService,
              IExporterService exporterService)
        {
            this.employeeService = employeeService;
            this.organizationUnitService = organizationUnitService;
            this.exporterService = exporterService;
        }


        [HttpPost]
        public JsonResult FilterEmployee(string term, string Id)
        {
            OrganizationUnit organizationUnit = null;
            if (Id != "0")
            {
                string OrganizationId = Id;
                organizationUnit = organizationUnitService.FindById(int.Parse(OrganizationId));
            }
            List<Employee> employeeList = (List<Employee>)employeeService.FindByKey(term, organizationUnit);
            List<object> data = new List<object>();
            foreach (Employee employee in employeeList)
            {
                if (employee.Person.Phones.Where<Phone>(model => model.Label == PhoneLabel.Tel).Count() == 0)
                {
                    data.Add(new
                    {
                        value = employee.Id,
                        label = employee.Person.Name,
                        phone = employee.Person.Phones.AsQueryable<Phone>().Where<Phone>(model => model.Label == PhoneLabel.Mobile).First<Phone>().Value,
                        telPhone = ""
                    });
                }
                else
                {
                    data.Add(new
                    {
                        value = employee.Id,
                        label = employee.Person.Name,
                        phone = employee.Person.Phones.AsQueryable<Phone>().Where<Phone>(model => model.Label == PhoneLabel.Mobile).First<Phone>().Value,
                        telPhone = employee.Person.Phones.Where<Phone>(model => model.Label == PhoneLabel.Tel).First<Phone>().Value
                    });
                }
            }

            //var data = from c in employeeList
            //           select new { value = c.Id, label = c.Person.Name, phone = c.Person.Phones.AsQueryable<Phone>().Where<Phone>(model => model.Label == PhoneLabel.Mobile).First<Phone>().Value,
            //                        telPhone = c.Person.Phones.AsQueryable<Phone>().Where<Phone>(model => model.Label == PhoneLabel.Tel).First<Phone>().Value
            //           };
            return Json(data, JsonRequestBehavior.AllowGet);
        }


    }
}